#ifndef __HTTP_REQUEST_H__
#define __HTTP_REQUEST_H__

// #include "Log/Log.h"
#include <regex>
#include <unordered_map>
#include <unordered_set>
#include <cerrno>

#include "sqlconnRAII.h"
#include "Log.h"
#include "BlockQueue.h"

class HttpRequest
{
    enum PARSE_STATE
    {
        REQUEST_LINE, // 正在解析请求首行
        HEADERS,      // 头
        BODY,         // 体
        FINISH,       // 完成
    };

public:
    enum HTTP_CODE {
        NO_REQUEST = 0,
        GET_REQUEST,
        BAD_REQUEST,
        NO_RESOURSE,
        FORBIDDENT_REQUEST,
        FILE_REQUEST,
        INTERNAL_ERROR,
        CLOSED_CONNECTION,
    };
    
    HttpRequest() { Init(); }
    ~HttpRequest() = default;


    bool parse(Buffer& buff);
    void Init();
    
    
    std::string path() const;//
    std::string& path();//
    std::string method() const;//
    std::string version() const;//
    std::string GetPost(const std::string& key) const;//
    std::string GetPost(const char* key) const;//

    bool IsKeepAlive() const;
private:
    bool ParseRequestLine_(const std::string& line);//
    void ParseHeader_(const std::string& line);//
    void ParseBody_(const std::string& line);//

    void ParsePath_();//
    void ParsePost_();//
    void ParseFromUrlencoded_(); //


    //
    static bool UserVerify(const std::string& name, const std::string& pwd, bool isLogin);


    static const std::unordered_set<std::string> DEFAULT_HTML;  // 默认的网页
    static const std::unordered_map<std::string, int> DEFAULT_HTML_TAG; 
    //
    static int ConverHex(char ch);  // 转换成十六进制

private:
    PARSE_STATE state_;
    std::string method_, path_, version_, body_;
    std::unordered_map<std::string, std::string> header_;
    std::unordered_map<std::string, std::string> post_;
};

#endif